
var user = getCookie("username");
// console.log(name);
$.get("./goodsAndShoppingCart/getShoppingCart.php", { "vipName": user}, function (data) {+
    Title(data);
    gwc();
}, "json");
function Title(data) {
    let str = '';
    // let arr = [];
    for (let i = 0; i < data.length; i++) {
        str += `<tr>
        <td colspan="2">
            <div class="check l"><input type="checkbox" checked></div>
            <div class="p-pic l">
                <img src="${data[i].goodsImg}" alt=""></div>
            <div class="p-info l">
                <p>明星系列</p>
                <p><a href="https://www.clarins.com.cn/face/double-serum-set-50ml">${data[i].goodsName}</a></p>
                <p>${data[i].goodsDesc}</p>
                <p class="spec">容量：50ml <span>编辑</span></p>
            </div>
        </td><td class="price">￥<span>${data[i].goodsPrice}</span></td>
        <td>
            <button class="prve">-</button>
            <span style="display: inline-block;width: 20px; height: 20px;" class="itxt">1</span>
            <button class="next">+</button> </td>
        <td class="total" style="color: #000;">￥<span>${data[i].goodsPrice}</span> </td>
        <td><input type="button" value="删除" class="delete"><span style="display:none">${data[i].goodsId}</span></td>
       </tr>`;
        $(".tbod").html(str);
    }
};  
function gwc() {
    // 增
    $(".next").click(function () {
        var n = $(this).siblings(".itxt").html()
        n++;
        $(this).siblings(".itxt").html(n);
        var p = $(this).parent().siblings(".price").children("span").html();
        // console.log(p);
        var price = (p * n).toFixed(2);
        $(this).parent().siblings(".total").children("span").html(price);
        getSum();
    });
    // 减
    $(".prve").click(function () {
        var n = $(this).siblings(".itxt").html()
        if (n == 1) {
            return false;
        }
        n--;
        $(this).siblings(".itxt").html(n);
        var p = $(this).parent().siblings(".price").children("span").html();
        // console.log(p);
        var price = (p * n).toFixed(2)//保留两位小数
        $(this).parent().siblings(".total").children("span").html(price);
        getSum()
    });
    // 计算总价和总件
    function getSum() {
        var count = 0;
        var money = 0;
        for (let i = 0; i < $(".itxt").length; i++) {
            count += Number($(".itxt").eq(i).html());
        }
        for (let i = 0; i < $(".total span").length; i++) {
            money += Number($(".total span").eq(i).html());
        }
        $("h3").children().html(count);
        $(".goods-num").html(count);
        $(".goods-price").html(money);
    }
    getSum();
    // 删除
    $(".delete").click(function () {
        $(this).parents().eq(1).remove();
        let id = $(this).siblings().html();
        $.get("./goodsAndShoppingCart/deleteGoods.php", {
            "vipName": user,
            "goodsId": id
        }, function () {
        });
        getSum();
    });

}
